<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>tomcat连接常用数据库的用法 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>tomcat连接常用数据库的用法</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<h2 id="一、用于数据库连接的术语："><a href="#一、用于数据库连接的术语：" class="headerlink" title="一、用于数据库连接的术语："></a>一、用于数据库连接的术语：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;JDBC：（Java database connectivity）是基于java数据访问技术的一个API通过客户端访问服务器的数据库，是一个面向关系型数据库并提供一种方法查询和更新数据库；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;JNDI：(Java naming and directory interface)JNDI服务提供了对应用程序命名和目录功 能的一种用java程序编写的基于API的java平台；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DataSource：是一个通过JDBC API访问关系型数据库的java对象，当与JNDI整合并在JDNI 名称服务中注册后能更好的工作；</p>
<h2 id="二、tomcat连接常用数据库的操作步骤："><a href="#二、tomcat连接常用数据库的操作步骤：" class="headerlink" title="二、tomcat连接常用数据库的操作步骤："></a>二、tomcat连接常用数据库的操作步骤：</h2><h3 id="Tomcat配置Oracle-DataSource："><a href="#Tomcat配置Oracle-DataSource：" class="headerlink" title="Tomcat配置Oracle DataSource："></a>Tomcat配置Oracle DataSource：</h3><ol>
<li>在server.xml全局文件中定义如下内容：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">&lt;GlobalNamingResources&gt; </div><div class="line">&lt;!-- Editable user database that can also be used by </div><div class="line">UserDatabaseRealm to authenticate users--&gt; </div><div class="line">&lt;Resource name=<span class="string">"jdbc/tomcat7"</span> auth=<span class="string">"Container"</span> </div><div class="line"><span class="built_in">type</span>=<span class="string">"javax.sql.DataSource"</span> </div><div class="line">driverClassName=<span class="string">"oracle.jdbc.OracleDriver"</span> </div><div class="line">url=<span class="string">"jdbc:oracle:thin:@127.0.0.1:1521:test"</span> </div><div class="line">description=<span class="string">"test database for tomcat 7"</span> </div><div class="line">Configuration and Deployment </div><div class="line">[ 46 ] </div><div class="line">username=<span class="string">"admin"</span> password=<span class="string">"admin"</span> maxActive=<span class="string">"20"</span> maxIdle=<span class="string">"10"</span> </div><div class="line">maxWait=<span class="string">"-1"</span>/&gt; </div><div class="line">&lt;/GlobalNamingResources&gt;</div></pre></td></tr></table></figure>
<ol>
<li><p><a href="http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html" target="_blank" rel="external">http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html</a> 下载Oracle JDBC驱动程序类并放在CATALINA_HOME/lib/目录下，tomcat默认只接 受.jar结尾的类，如果是zip压缩格式需要将其重名为.jar结尾，然后放到 CATALINA_HOME/lib/目录下；</p>
</li>
<li><p>在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义，示例如下：</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;resource-ref&gt; </div><div class="line">&lt;description&gt;Oracle Datasource <span class="keyword">for</span> tomcat &lt;/description&gt; </div><div class="line">&lt;res-ref-name&gt;jdbc/tomcat7 &lt;/res-ref-name&gt; </div><div class="line">&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; </div><div class="line">&lt;res-auth&gt;Container&lt;/res-auth&gt; </div><div class="line">&lt;/resource-ref&gt;</div></pre></td></tr></table></figure>
<ol>
<li>开发人员在代码中引用JNDI并连接到数据库；</li>
</ol>
<h3 id="Tomcat配置mysql-DataSource："><a href="#Tomcat配置mysql-DataSource：" class="headerlink" title="Tomcat配置mysql DataSource："></a>Tomcat配置mysql DataSource：</h3><ol>
<li>在server.xml全局文件中定义如下内容：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;Resource name=<span class="string">"jdbc/tomcat7"</span> auth=<span class="string">"Container"</span> </div><div class="line"><span class="built_in">type</span>=<span class="string">"javax.sql.DataSource"</span> </div><div class="line">maxActive=<span class="string">"100"</span> maxIdle=<span class="string">"30"</span> maxWait=<span class="string">"10000"</span> </div><div class="line">username=<span class="string">"tomcatuser"</span> password=<span class="string">"tomcat"</span> </div><div class="line">driverClassName=<span class="string">"com.mysql.jdbc.Driver"</span> </div><div class="line">url=<span class="string">"jdbc:mysql://localhost:3306/tomcat7"</span>/&gt;</div></pre></td></tr></table></figure>
<ol>
<li>在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义，示例如下：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">&lt;web-app xmlns=<span class="string">"http://java.sun.com/xml/ns/j2ee"</span> </div><div class="line">xmlns:xsi=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span> </div><div class="line">xsi:schemaLocation=<span class="string">"http://java.sun.com/xml/ns/j2ee </span></div><div class="line"><span class="string">http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span> </div><div class="line">version=<span class="string">"2.4"</span>&gt; </div><div class="line">&lt;description&gt;Tomcat 7 <span class="built_in">test</span> DB&lt;/description&gt; </div><div class="line">&lt;resource-ref&gt; </div><div class="line">&lt;description&gt;DB Connection&lt;/description&gt; </div><div class="line">&lt;res-ref-name&gt;jdbc/tomcat7&lt;/res-ref-name&gt; </div><div class="line">&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; </div><div class="line">&lt;res-auth&gt;Container&lt;/res-auth&gt; </div><div class="line">&lt;/resource-ref&gt; </div><div class="line">&lt;/web-app&gt;</div></pre></td></tr></table></figure>
<ol>
<li><p><a href="http://dev.mysql.com/downloads/" target="_blank" rel="external">http://dev.mysql.com/downloads/</a> 下载MYSQL JDBC驱动程序类并放在 CATALINA_HOME/lib/目录下，tomcat默认只接受.jar结尾的类，如果是zip压缩格式需 要将其重名为.jar结尾，然后放到CATALINA_HOME/lib/目录下；</p>
</li>
<li><p>对连接tomcat的用户授予全部权限，格式如下：</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">mysql&gt; GRANT ALL PRIVILEGES ON *.* TO tomcatuser@localhost </div><div class="line">IDENTIFIED BY <span class="string">'tomcat7'</span> WITH GRANT OPTION; </div><div class="line">mysql&gt; create database tomcat7; </div><div class="line">mysql&gt; use tomcat7; </div><div class="line">mysql&gt; create table testdata ( id int not null auto_increment </div><div class="line">primary key,foo varchar(25), bar int);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：用户密码一定不要为空，否则会造成连接tomcat认证错误； </p>
<h3 id="Tomcat配置Postgresql-DataSource："><a href="#Tomcat配置Postgresql-DataSource：" class="headerlink" title="Tomcat配置Postgresql DataSource："></a>Tomcat配置Postgresql DataSource：</h3><ol>
<li>在server.xml全局文件中定义如下内容：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;Resource name=<span class="string">"jdbc/tomcat7"</span> auth=<span class="string">"Container"</span> </div><div class="line"><span class="built_in">type</span>=<span class="string">"javax.sql.DataSource"</span> </div><div class="line">driverClassName=<span class="string">"org.postgresql.Driver"</span> </div><div class="line">url=<span class="string">"jdbc:postgresql://127.0.0.1:5432/tomcat7"</span> </div><div class="line">username=<span class="string">"tomcat7"</span> password=<span class="string">"tomcat"</span> maxActive=<span class="string">"20"</span> maxIdle=<span class="string">"10"</span> </div><div class="line">maxWait=<span class="string">"-1"</span>/&gt;</div></pre></td></tr></table></figure>
<ol>
<li><p><a href="http://jdbc.postgresql.org/download.html" target="_blank" rel="external">http://jdbc.postgresql.org/download.html</a> 下载PostgreSQL JDBC驱动类并放在 CATALINA_HOME/lib/目录下，tomcat默认只接受.jar结尾的类，如果是zip压缩格式需 要将其重名为.jar结尾，然后放到CATALINA_HOME/lib/目录下；</p>
</li>
<li><p>在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义，示例如下：</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;resource-ref&gt; </div><div class="line">&lt;description&gt;postgreSQL Tomcat datasource &lt;/description&gt; </div><div class="line">&lt;res-ref-name&gt;jdbc/tomcat7 &lt;/res-ref-name&gt; </div><div class="line">&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; </div><div class="line">&lt;res-auth&gt;Container&lt;/res-auth&gt; </div><div class="line">&lt;/resource-ref&gt;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Tomcat/11. tomcat连接常用数据库的用法/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
